package com.example.rtspclientdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        //System.loadLibrary("rtspclient");
        //System.loadLibrary("live555");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());

        RtspInit();

        RtspOpenUrl(0, "rtsp://admin:123456@10.58.123.221:554");

        new Thread(){
            @Override
            public void run() {
                super.run();
                byte[] frame = new byte[1024*1024];
                int[] len = new int[1];
                int[] encodetype = new int[1];
                int[] frametype = new int[1];
                while(true) {
                    RtspGetFrame(0, frame, len, encodetype, frametype);
                    if(len[0] > 0) {
                        Log.d("java-test", "len = " + len[0]);
                    }
                    else {
                        try {
                            sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
    public native int RtspInit();
    public native int RtspDeInit();
    public native int RtspOpenUrl(int _s32Chn, String _strUrl);
    public native int RtspCloseUrl(int _s32Chn);
    public native int RtspGetFrame(int _s32Chn, byte[] _FrameData, int[] _FrameLen, int[] _EncodeType, int[] _FrameType);
}